package com.itheima.reggie.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//菜品管理
@Transactional
@Slf4j
@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    // 分页条件查询
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.构建菜品条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        //2.构建分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //3.查询分页
        page = dishMapper.selectPage(page, qw);
        //4.遍历菜品list集合
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            //4-1查询口味
            //构建口味条件
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DishFlavor::getDishId,dish.getId());
            //查询口味列表
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(lqw);
            //封装到菜品实类中
            dish.setFlavors(flavorList);
            //查询分类
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());
        }

        //5.返回分页对象
        return page;
    }

    //新增套餐时回显菜品
    @Override
    public List<Dish> findListBySetmeal(Long categoryId, String name) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //如果name是null就执行查询菜品
        qw.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        //如果categoryid是null就执行模糊查询
        qw.eq(categoryId != null,Dish::getCategoryId, categoryId);

        //调用mapper回显菜品或模糊查询
        return dishMapper.selectList(qw);
    }


    //新增菜品
    @Override
    public void save(Dish dish) {
        //1.直接保存菜品信息
        log.info("新增之前id：{}",dish.getId());
        dishMapper.insert(dish);
        log.info("新增之后id：{}",dish.getId());  //主键返回
        //2.遍历前端提供的口味列表
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor flavor : flavors) {
            //关联菜品id
            flavor.setDishId(dish.getId());
            //保存数据库
            dishFlavorMapper.insert(flavor);
        }
    }


    //菜品回显
    @Override
    public Dish findById(Long id) {
        //1.根据id查询菜品基本信息
        Dish dish = dishMapper.selectById(id);
        //2.根据id查询该菜品的口味列表
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,id);
        //3.将口味列表设置到菜品中
        List<DishFlavor> flavorList = dishFlavorMapper.selectList(qw);
        dish.setFlavors(flavorList);
        //4.返回菜品
        return dish;
    }


    //菜品修改
    @Override
    public void update(Dish dish) {
        //直接修改菜品基本信息
        dishMapper.updateById(dish);
        //先根据菜品id删除旧口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dish.getId());
        //根据条件删除
        dishFlavorMapper.delete(qw);
        //遍历新口味 关联菜品id 保存数据库
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }
    }

    //启停售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);  //where id in(xxx)
        //构建修改实体
        Dish dish = new Dish();
        dish.setStatus(status); // set status = 0|1
        //执行数据库
        dishMapper.update(dish,qw);
    }


    //删除菜品
    @Override
    public void deleteBatchIds(List<Long> ids) {
        //1.先判断菜品是否处于停售状态 select count(*) from dish where status = 1 and id in (xx,xx,xx)
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);  //id in (xx,xx,xx)
        qw.eq(Dish::getStatus,1); //status = 1
        Integer count = dishMapper.selectCount(qw);
        if (count>0){
            throw new CustomException("删除的菜品状态必须为已停售！");
        }

        //删除菜品 delete from dish where id in(xx,xx,xx)
        dishMapper.deleteBatchIds(ids);

        //删除口味 delete from dish_flavor where dish_id in(xx,xx,xx)
        LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
        dqw.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(dqw);
    }

    // 根据分类id+状态查询菜品列表
    @Override
    public List<Dish> findList(Long categoryId, Integer status) {
        //1.查询菜品列表
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        qw.eq(Dish::getStatus,status);
        //mapper查询
        List<Dish> list = dishMapper.selectList(qw);
        //2.遍历菜品列表
        for (Dish dish : list) {
            //根据菜品的id查询口味
            LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
            dqw.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(dqw);
        //3.查询口味列表并封装到菜品中
            dish.setFlavors(flavorList);
        }
        return list;
    }


}
